import { Component, OnInit, Input, Output, OnChanges, SimpleChanges } from '@angular/core';
import { optionsData, seriesData } from "./pie-echarts.model";

@Component({
	selector: 'app-assess-pie-echart',
	templateUrl: './assess-pie-echart.component.html',
	styleUrls: ['./assess-pie-echart.component.css']
})
export class AssessPieEchartComponent implements OnInit {

	/**
	 * echarts实例对象
	 * 
	 * @private
	 * @type {*}@memberof CompareEchartsComponent
	 */
	private echartsIntance: any = null;

	/**
	 * 父组件输入echarts参数
	 * 
	 * @private
	 * @type {*}@memberof CompareEchartsComponent
	 */
	@Input() series: seriesData[];
	@Input() active: number;

	options: optionsData;
	tooltip: object;
	legend: object;
	isFirst: boolean = true;

	constructor() { }

	ngOnInit() {
	}

	ngOnChanges(changes: SimpleChanges) {
		if (changes['series'] && changes['series'].currentValue) {
			if (!this.isFirst) {
				this.updateEchart();
			} else {
				this.isFirst = false;
				this.initOptions();
			}
		}

		// if (changes['legend'] && changes['legend'].currentValue) {
		// 	this.options.legend = this.legend;
		// 	if (!this.isFirst && this.echartsIntance) {
		// 		this.options.series = this.series;
		// 		this.echartsIntance.setOption(this.options, true);
		// 	} else {
		// 		this.isFirst = false;
		// 		this.initOptions();
		// 	}
		// }
	}

	initOptions() {
		this.tooltip = {
			trigger: 'item',
			formatter: "{a} <br/>{b} : {c} ({d}%)"
		},
		this.legend = {
			left: 'center',
			data: [
				{
					name: '实发电量',
					textStyle: { color: '#22b14c' }
				},
				{
					name: '性能损失',
					textStyle: { color: '#a349a4' }
				},
				{

					name: '停机损失',
					textStyle: { color: '#ff7f27' }
				},
				{
					name: '限电损失',
					textStyle: { color: '#00a2e8' }
				},
				{
					name: '坏点损失',
					textStyle: { color: '#FF0000' }
				}
			]
		}
		if (this.options == null) {
			this.options = {
				tooltip: this.tooltip,
				legend: this.legend,
				series: this.series
			};
		}
	}
	/**
	 * 初始化echarts实例
	 * 
	 * 
	 * @memberof CompareEchartsComponent
	 */
	onChartInit(ec: any) {
		this.echartsIntance = ec;
		this.updateEchart();
	}
	/**
	 * 更新echarts实例数据
	 * 
	 * 
	 * @memberof CompareEchartsComponent
	 */
	updateEchart() {
		if (this.echartsIntance != null) {
			this.options.tooltip = this.tooltip;
			this.options.series = this.series;
			this.echartsIntance.setOption(this.options);
		};
	}

}



// WEBPACK FOOTER //
// ./src/app/module/performance/farm-power-assess/assess-pie-echart/assess-pie-echart.component.ts